package vip.xiaonuo.hontu.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import vip.xiaonuo.core.timer.TimerTaskRunner;
import vip.xiaonuo.facedoor.modular.device.entity.DeviceCategory;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.modular.clustertask.service.ClusterTaskService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定时同步聚类结果
 * 10分钟执行一次
 */
@Component
@Slf4j
public class ClusterResultSyncTask implements TimerTaskRunner {

    @Resource
    private DeviceBaseService deviceBaseService;

    @Resource
    private ClusterTaskService clusterTaskService;

    @Override
    public void action(String jobParams, String taskId) {
        syncTaskResult();
    }

    @Override
    public void executeXxlJob() throws InterruptedException {
        syncTaskResult();
    }

    private void syncTaskResult(){
        List<DeviceBase> devices = deviceBaseService.list(new LambdaQueryWrapper<DeviceBase>()
                .eq(DeviceBase :: getDeviceType, DeviceCategory.SUB_TYPE_AI_HT+"")
                .eq(DeviceBase :: getDeviceState , "1"));

        for(DeviceBase device : devices){
            clusterTaskService.syncTaskResult(device);
        }
    }
}
